const app = getApp();
Page({
  data: {
    goods: [],
    totalCheck: false,
    total: 0,
    canDel: false,
    error_img: '/images/empty.jpg',
  },
  onShow: function () {
    let that = this;
    that.setData({
      pageShow: true,
    })
    that.getInfo();
  },
  getInfo(){
    let that = this;
    app.request('shopcar/index',{},'GET',false,function(res){
      wx.stopPullDownRefresh();
      if (res.data.data.list.goods) {
        res.data.data.list.goods.map((v, k) => {
          v.specType = 0;
          v.checked = false
        })
      }
      if (res.data.data.list.package) {
        for (let i in res.data.data.list.package) {
          res.data.data.list.package[i].goods.map((v, k) => {
            v.checked = false
          })
          res.data.data.list.package[i]['specType'] = 1;
        }
      }
      let pageEmpty = false;
      if(res.data.data.list.goods.length == 0 && res.data.data.list.package.length == 0){
        pageEmpty = true;
      }
      that.setData({
        pageEmpty,
        pageShow: true,
        goods: res.data.data.list,
      })
    })
  },
  goodsCheck(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let type = e.currentTarget.dataset.type;
    let goods = that.data.goods;
    let totalCheck = that.data.totalCheck;
    switch (type) {
      case 'goods':
        goods.goods[index].checked = goods.goods[index].checked ? false : true;
        break;
      case 'package':
        goods.package[index].checked = goods.package[index].checked ? false : true;
        goods.package[index].goods.map((v, k) => {
          v.checked = goods.package[index].checked ? true : false;
        })
        break;
      case 'menu':
        let idx = e.currentTarget.dataset.idx;
        goods.package[index].goods[idx].checked = goods.package[index].goods[idx].checked ? false : true;
        goods.package[index].checked = true;
        goods.package[index].goods.map((v,k)=>{
          v.checked ? '' : goods.package[index].checked=false;
        })
        break;
      case 'all':
        totalCheck = totalCheck?false:true;
        goods.goods.map((v,k)=>{
          v.checked = totalCheck ? true : false;
        })
        for (let i in goods.package){
          goods.package[i].checked = totalCheck ? true : false;
          goods.package[i].goods.map((v, k) => {
            v.checked = totalCheck ? true : false;
          })
        }
        break;
    }
    that.judgeTotal(goods)
  },
  // 判断全选是否选中
  judgeTotal(goods) {
    let that = this;
    let total = new Number;
    let totalCheck = true;
    let shopcar = [];
    goods.goods.map((v, k) => {
      if(v.checked){
        total += parseInt(v.goods_price*100) * parseInt(v.total_num);
        shopcar.push(v.shopcar_id)
      }else{
        totalCheck = false
      }
    })
    for(let i in goods.package){
      goods.package[i].checked ? '' : totalCheck = false;
      goods.package[i].goods.map((v, k) => {
        if (v.checked) {
          total += parseInt(v.goods_price*100) * parseInt(v.total_num);
          shopcar.push(v.shopcar_id)
        }
      })
    }
    total = Number((total/100)).toFixed(2);
    that.setData({
      totalCheck: totalCheck,
      total: total,
      goods: goods,
      shopcar,
      canDel:shopcar.length>0?true:false,
    })
  },
  numChange(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let type = e.currentTarget.dataset.type;
    let change = e.currentTarget.dataset.change;
    let goods = that.data.goods;
    let idx = e.currentTarget.dataset.idx;
    let shopcar_id = type == 'goods' ? goods.goods[index].shopcar_id : goods.package[index].goods[idx].shopcar_id;
    wx.showLoading({
      title: '',
      icon: 'loading',
      mask: true
    })
    app.request(change == 'reduce' ? 'shopcar/delNumber' : 'shopcar/addNumber', {
      shopcar_id,num: 1 
    },'GET',false,function(res){
      wx.hideLoading()
      switch (type) {
        case 'goods':
          if (change == 'reduce') {
            goods.goods[index].total_num == 1 ? 1 : goods.goods[index].total_num--
          } else {
            goods.goods[index].total_num++
          }
          that.judgeTotal(goods)
          break;
        case 'package':

          if (change == 'reduce') {
            goods.package[index].goods[idx].total_num == 1 ? 1 : goods.package[index].goods[idx].total_num--
          } else {
            goods.package[index].goods[idx].total_num++
          }
          that.judgeTotal(goods);
          break;
      }
    })
  },

  // 输入
  onBlur(e){
    let that = this;
    let { stock,index,idx=0,type } = e.currentTarget.dataset;
    let { goods } = that.data;
    let shopcar_id = type == 'goods' ? goods.goods[index].shopcar_id : goods.package[index].goods[idx].shopcar_id;
    let num;
    if(type == 'goods'){
      if(e.detail.value < stock){
        goods.goods[index].total_num = e.detail.value;
      }else{
        goods.goods[index].total_num = stock;
      }
      num = goods.goods[index].total_num
    }else{
      if(e.detail.value < stock){
        goods.package[index].goods[idx].total_num = e.detail.value;
      }else{
        goods.package[index].goods[idx].total_num = stock;
      }
      num = goods.package[index].goods[idx].total_num
    }
    wx.showLoading({
      title: '',
      icon: 'loading',
      mask: true
    })
    app.request('shopcar/addNumber', {
      shopcar_id,num,is_input: 1
    },'GET',false,function(res){
      wx.hideLoading()
      that.judgeTotal(goods)
    })
  },
  // 删除
  goodsDel(){
    let that = this;
    let { shopcar } = that.data;
    if(shopcar.length==0){
      wx.showToast({
        title: '请选择商品!',
        icon: 'none',
        mask: true
      })
      return false
    }
    wx.showModal({
      title: '确认删除',
      content: '确认删除商品吗?',
      success(res){
        if(res.confirm){
          let shopcar_id = shopcar.toString();
          app.request('shopcar/delShopcar', {
            shopcar_id
          },'GET',false,function(res){
            wx.showToast({
              title: '删除成功',
            })
            that.getInfo();
          })
        }
      }
    })
    
  },
  //结算
  sure(){
    let that = this;
    let shopcar = that.data.shopcar;
    wx.navigateTo({
      url: '/pages/order/orderSure/orderSure?shopcar=' + JSON.stringify(shopcar),
    })
  },
  onPullDownRefresh: function () {
    this.getInfo();
  },
  onShareAppMessage: function () {
    return{
      path: '/pages/index/index'
    }
  }
})